﻿using System;
using System.Windows.Forms;

namespace UserData
{
    public partial class Control_ScrewData : UserControl,Entity.Interface_UserControl
    {
        public class TestResult
        {
            public TestInfo str_StationName { get; set; } = new TestInfo { Name = "站位名称", Value = "", Max = " ", Min = " " };
            public TestInfo n_Index { get; set; } = new TestInfo { Name = "数据编号", Value = "", Max = " ", Min = " " };
            public TestInfo d_Torque { get; set; } = new TestInfo { Name = "扭矩", Value = "", Max = " ", Min = " " };
            public TestInfo d_Angle { get; set; } = new TestInfo { Name = "角度", Value = "", Max = " ", Min = " "};
            public TestInfo d_High { get; set; } = new TestInfo { Name = "高度", Value = "", Max = " ", Min = " "};
            public TestInfo b_Result { get; set; } = new TestInfo { Name = "结果", Value = "", Max = " ", Min = " " };
            public TestInfo strTime { get; set; } = new TestInfo { Name = "时间", Value = "", Max = " ", Min = " " };

        }
        public Control_ScrewData()
        {
            InitializeComponent();
        }

        private void Control_ScrewData_Load(object sender, EventArgs e)
        {
        }
        public void Init()
        {
            ScrewDataMgr.GetInstance().Init();
            ScrewDataMgr.GetInstance().OnTestReflushScrewdelegate += AddScrewData;
        }
        private void AddScrewData(ScrewData data)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => AddScrewData(data)));
            }
            else
            {
                int ret= dg_ScrewData.Rows.Add(data.strName, data.n_Index, data.d_Torque, data.d_Angle, data.d_High, data.b_Result, data.strTime);
                dg_ScrewData.SelectedIndex = ret;
            }
        }

        private void 清除数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ScrewDataMgr.GetInstance().Function_Clear();
            dg_ScrewData.Rows.Clear();
        }

        private void 导出CSVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog file = new SaveFileDialog();
            string temp_path = "D:\\";

            file.InitialDirectory = temp_path;
            file.Filter = "(*.*)|*.csv";

            file.Title = " csv文件";
            if (file.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            temp_path = file.FileName;
            if (!temp_path.Contains(".csv"))
                return;
            for (int i = 0; i < dg_ScrewData.RowCount; i++)
            {
                TestResult result = new TestResult();
                result.str_StationName.Value = dg_ScrewData.Rows[i].Cells[0].Value.ToString();
                result.n_Index.Value = dg_ScrewData.Rows[i].Cells[1].Value.ToString();
                result.d_Torque.Value = dg_ScrewData.Rows[i].Cells[2].Value.ToString();
                result.d_Angle.Value = dg_ScrewData.Rows[i].Cells[3].Value.ToString();
                result.d_High.Value = dg_ScrewData.Rows[i].Cells[4].Value.ToString();
                result.b_Result.Value = dg_ScrewData.Rows[i].Cells[5].Value.ToString();
                result.strTime.Value = dg_ScrewData.Rows[i].Cells[6].Value.ToString();
                CSVHelper.Instance.SaveToCSVPath(temp_path, result);
            }


        }
    }
}
